/**
 * FileName: NewsViewModel
 * Author: Administrator
 * Date: 2020/12/14 0014 19:25
 * Description:
 */
package com.linkage.linkedlife.viewmodel;

import android.app.Application;
import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.arch.core.util.Function;
import androidx.databinding.ObservableField;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Transformations;

import com.clark.dingding.model.NewsData;
import com.clark.dingding.retrofit.repository.GankDataRepository;
import com.google.gson.Gson;
import com.linkage.linkedlife.utils.NetUtils;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @ClassName: NewsViewModel
 * @Description: java类作用描述
 * @Author: Clakr
 * @Date: 2020/12/14 0014 19:25
 */
public class NewsViewModel extends AndroidViewModel {

    private static final MutableLiveData ABSENT = new MutableLiveData();

    {
        //noinspection unchecked
        ABSENT.setValue(null);
    }

    //生命周期观察的数据
    private LiveData<NewsData> mLiveObservableData;
    //UI使用可观察的数据 ObservableField是一个包装类
    public ObservableField<NewsData> uiObservableData = new ObservableField<>();

    private final CompositeDisposable mDisposable = new CompositeDisposable();
    private String currentPage="1";
    private String pageSize="15";
    MutableLiveData<NewsData> applyData = new MutableLiveData<>();
    public NewsViewModel(@NonNull Application application) {
        super(application);
        Log.i("danxx", "GirlsViewModel------>");
        //这里的trigger为网络检测，也可以换成缓存数据是否存在检测


        mLiveObservableData = Transformations.switchMap(NetUtils.netConnected(application), new Function <Boolean, LiveData<NewsData>>() {
            @Override
            public LiveData<NewsData> apply(Boolean isNetConnected) {

                Log.i("danxx", "apply------>");
                if (!isNetConnected) {
                    return ABSENT; //网络未连接返回空
                }


                GankDataRepository.getNewsDataRepository(pageSize, currentPage)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<NewsData>() {
                            @Override
                            public void onSubscribe(Disposable d) {
                                mDisposable.add(d);
                            }

                            @Override
                            public void onNext(NewsData value) {
                                Log.d("clark","getdata success");
                                Log.d("clark",new Gson().toJson(value));
                                applyData.setValue(value);
                            }

                            @Override
                            public void onError(Throwable e) {
                                e.printStackTrace();
                            }

                            @Override
                            public void onComplete() {
                            }
                        });


                return applyData;
            }
        });

    }
    public  void refresh(Context context, String mpageSize, String mcurrentPage){

                GankDataRepository.getNewsDataRepository(mpageSize, mcurrentPage)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<NewsData>() {
                            @Override
                            public void onSubscribe(Disposable d) {
                                mDisposable.add(d);
                            }

                            @Override
                            public void onNext(NewsData value) {
                                Log.d("clark","getdata success");
                                Log.d("clark",new Gson().toJson(value));
//                                applyData.setValue(value);
                                applyData.postValue(value);
                            }

                            @Override
                            public void onError(Throwable e) {
                                e.printStackTrace();
                            }

                            @Override
                            public void onComplete() {
                            }
                        });


    }


    /**
     * LiveData支持了lifecycle生命周期检测
     * @return
     */

    public LiveData<NewsData> getLiveObservableData(String mPageSize, String mCurrentPage) {

        return mLiveObservableData;
    }

    /**
     * 设置
     * @param product
     */
    public void setUiObservableData(NewsData product) {
        this.uiObservableData.set(product);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        Log.d("clark","========NewsViewModel--onCleared=========");
        mDisposable.clear();
    }
}